package com.jsmile.cloud.authc.sys.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jsmile.cloud.authc.api.req.sys.ReqSysDictPageQuery;
import com.jsmile.cloud.authc.api.req.sys.ReqSysDictQuery;
import com.jsmile.cloud.authc.api.req.sys.ReqSysDictUpdate;
import com.jsmile.cloud.authc.api.res.ResSysDict;
import com.jsmile.cloud.authc.sys.SysDictApplicationService;
import com.jsmile.mail.security.controller.AuthcController;
import com.jsmile.mail.security.handler.JsmileContext;
import com.jsmile.mall.api.JSmileResult;
import com.jsmile.mall.api.annotation.CurrentContext;
import com.jsmile.mall.api.constants.JSmileConstant;
import com.jsmile.mall.api.enums.LogTypeEnum;
import com.jsmile.mall.api.to.JsmilePage;
import com.jsmile.mall.log.annotation.JsmileLog;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 龚亮
 * @version 1.0
 * @desc 控制器
 * @date 2018年08月25日 04:57:24
 */
@RestController
@Api(tags = "字典管理")
@Slf4j
@AllArgsConstructor
@RequestMapping("/sys/dict")
public class SysDictController extends AuthcController {

    private final SysDictApplicationService sysDictApplicationService;

    @RequestMapping(value = "/findAll", method = RequestMethod.POST)
    @ApiOperation(value = "分页获取字典列表")
    public JSmileResult<JsmilePage<ResSysDict>> getPageList(@RequestBody @Valid ReqSysDictPageQuery reqSysDictPageQuery, @CurrentContext JsmileContext jsmileContext) {
        reqSysDictPageQuery.setCurTenantId(jsmileContext.getTenantId());
        IPage<ResSysDict> pages = sysDictApplicationService.getPageList(reqSysDictPageQuery);
        return JSmileResult.success(new JsmilePage(reqSysDictPageQuery.getPageIndex(), reqSysDictPageQuery.getPageSize(), pages.getTotal(), pages.getRecords()));
    }

    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    @ApiOperation(value = "获取字典列表")
    public JSmileResult<JsmilePage<ResSysDict>> getList(@RequestBody @Valid ReqSysDictQuery reqSysDictQuery, @CurrentContext JsmileContext jsmileContext) {
        reqSysDictQuery.setCurTenantId(jsmileContext.getTenantId());
        List<ResSysDict> pages = sysDictApplicationService.getList(reqSysDictQuery);
        return JSmileResult.success(pages);
    }

    @ApiOperation(value = "根据字典编码获取字典列表")
    @RequestMapping(value = "/findDictByCode", method = RequestMethod.POST)
    public JSmileResult<List<ResSysDict>> findDictByCode(@RequestBody @Valid ReqSysDictQuery reqSysDictQuery, @CurrentContext JsmileContext jsmileContext) {
        reqSysDictQuery.setCurTenantId(jsmileContext.getTenantId());
        List<ResSysDict> list = sysDictApplicationService.findDictByCode(reqSysDictQuery);
        return JSmileResult.success(list);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(value = "新增字典")
    @JsmileLog(title = "用户中心->新增字典", type = LogTypeEnum.INSERT, systemId = JSmileConstant.SystemId.AUTHC)
    public JSmileResult saveDict(@RequestBody @Valid ReqSysDictUpdate reqSysDictUpdate, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.AUTHC);
        reqSysDictUpdate.setTenantId(jsmileContext.getTenantId());
        reqSysDictUpdate.setCreateBy(jsmileContext.getUserName());
        reqSysDictUpdate.setUpdateBy(jsmileContext.getUserName());
        if (!sysDictApplicationService.saveDict(reqSysDictUpdate)) {
            return JSmileResult.error("新增字典异常!");
        }
        return JSmileResult.success();
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "修改字典")
    @JsmileLog(title = "用户中心->修改字典", type = LogTypeEnum.UPDATE, systemId = JSmileConstant.SystemId.AUTHC)
    public JSmileResult updateDict(@RequestBody @Valid ReqSysDictUpdate reqSysDictUpdate, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.AUTHC);
        reqSysDictUpdate.setTenantId(jsmileContext.getTenantId());
        reqSysDictUpdate.setCreateBy(jsmileContext.getUserName());
        reqSysDictUpdate.setUpdateBy(jsmileContext.getUserName());
        if (!sysDictApplicationService.updateDict(reqSysDictUpdate)) {
            return JSmileResult.error("修改字典异常!");
        }
        return JSmileResult.success();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除字典")
    @JsmileLog(title = "用户中心->删除字典", type = LogTypeEnum.DELETE, systemId = JSmileConstant.SystemId.AUTHC)
    public JSmileResult deleteDict(@RequestBody @Valid ReqSysDictUpdate reqSysDictUpdate, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.AUTHC);
        reqSysDictUpdate.setTenantId(jsmileContext.getTenantId());
        if (!sysDictApplicationService.deleteDict(reqSysDictUpdate)) {
            return JSmileResult.error("删除字典异常!");
        }
        return JSmileResult.success();
    }
}
